
import { get,post } from '../../http/axios'

export default {
    // 命名空间
    namespaced:true,
    // 状态 -> data
    state:{
        art: {},
        // article:{},
        category:{},
        datalist: {}

    },
      //过滤器  处理stata中的变量
      getters: {
        confirmed(state) {
            console.log(state);
            
            if (state.datalist.list) {
                // console.log(state.datalist);
                var res=0;
                state.datalist.list.forEach((item) => {
                    // console.log(item);
                    res += item.confirmed;
                });
                // console.log(res);

            }
            return res;
        },
        suspected(state) {
            // console.log(state);
            
            if (state.datalist.list) {
                // console.log(state.datalist);
                var res=0;
                state.datalist.list.forEach((item) => {
                    // console.log(item);
                    res += item.suspected;
                });
                // console.log(res);

            }
            return res;
        },
        dead(state) {
            // console.log(state);
            
            if (state.datalist.list) {
                // console.log(state.datalist);
                var res=0;
                state.datalist.list.forEach((item) => {
                    // console.log(item);
                    res += item.dead;
                });
                // console.log(res);

            }
            return res;
        },
        cure(state) {
            // console.log(state);
            
            if (state.datalist.list) {
                // console.log(state.datalist);
                var res=0;
                state.datalist.list.forEach((item) => {
                    // console.log(item);
                    res += item.cure;
                });
                // console.log(res);

            }
            return res;
        },
        severe(state) {
            // console.log(state);
            
            if (state.datalist.list) {
                // console.log(state.datalist);
                var res=0;
                state.datalist.list.forEach((item) => {
                    // console.log(item);
                    res += item.severe;
                });
                // console.log(res);

            }
            return res;
        },
        outside(state) {
            // console.log(state);
            
            if (state.datalist.list) {
                // console.log(state.datalist);
                var res=0;
                state.datalist.list.forEach((item) => {
                    // console.log(item);
                    res += item.outside;
                });
                // console.log(res);

            }
            return res;
        }
    },
    // 突变 -> 改变state中变量值的唯一方式
    mutations:{
        set_datalist(state, list) {

            state.datalist = list;
        },

        set_article(state,val){
            state.article = val;
        },
        set_art(state,val){
            state.art = val;
        },
        set_category(state,val){
            // console.log(val.data);
            state.category = val;
        }
    },
    // 动作 -> methods，发送请求
    actions:{
          async page_query_art({commit}, params) {
            let res = await get('/article/pageQuery', params)
            // context.commit('set_datalist', res.data.data)
            //{commit}直接解构
            commit('set_art', res.data.data);
            // console.log(res);
            

        },
        async page_query(context, params) {
            console.log(212);
            
            let res = await get('/epidemic/pageQuery', params)
            console.log(res.data.list);
            //把结果给突变里面的方法
            context.commit('set_datalist', res.data.data)

        },
        async page_query_article({commit},params) {
            let res = await get('/article/pageQuery',params);
            // console.log(res.data);
           commit('set_art', res.data)
            // 调用突变中的方法
        //    return res;
        },
        
        async findAllCategory({commit}) {
            let res = await get('/category/findAll');
            commit('set_category',res.data);
        },

        async Category({commit},params) {
            let res = await get('/category/findAll',params);
            commit('set_category',res.data);
        },

        async deleteById({commit},params) {
            let res = await get('/category/deleteById',params);
            return res;
        },

        async saveOrUpdate({commit},params) {
            let res = await post('/category/saveOrUpdate',params);
            return res;
        },
    }
}
